package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.AddJobBean;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.mvp.presenter.AddJobPresenter;
import com.android.infantschool.model.mvp.presenter.AddJobPresenterImpl;
import com.android.infantschool.model.mvp.view.AddJobView;
import com.android.infantschool.util.DateUtil;
import com.android.infantschool.util.EdittorUtil;
import com.android.infantschool.util.ToastUtil;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @particulars
 * @time 2019\7\5 0005 15:52
 * @class describe
 */
public class NewHomeworkActivity extends BaseActivity implements AddJobView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.ed_content)
    public EditText edContent;
    @BindView(R.id.tv_content_size)
    public TextView tvContentSize;
    @BindView(R.id.tv_publish_date)
    public TextView tvPublishDate;
    @BindView(R.id.btn_submit)
    public Button btnSubmit;

    private AddJobPresenter jobPresenter = null;

    public static Intent newIntent(Activity activity) {
        Intent intent = new Intent(activity, NewHomeworkActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_homework);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        jobPresenter = new AddJobPresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("布置作业");
        tvPublishDate.setText(DateUtil.getDate(DateUtil.ymd));
        EdittorUtil.hideInput(this);

        edContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int length = s.toString().length();
                length = 500 - length;
                tvContentSize.setText(length + "/500");
            }
        });
    }

    @OnClick({R.id.btn_submit, R.id.img_back, R.id.tv_publish_date})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                String connect = edContent.getText().toString().trim();
                if (TextUtils.isEmpty(connect)) {
                    ToastUtil.showShortToast(this, "请输入作业内容");
                    return;
                }
                Map<String, String> map = new HashMap<>();
                map.put("token", AppConfig.getToken());
                map.put("content", connect);
                map.put("time", tvPublishDate.getText().toString());
                Log.e(TAG,"map:" + map.toString());
                jobPresenter.postAddJob(map);
                this.showLoading();
                break;
            case R.id.img_back:
                this.finish();
                break;
            case R.id.tv_publish_date:
                EdittorUtil.hideInput(this);
                showYearMonthPicker();
                break;
        }
    }

    @Override
    public void errorAddJob(String error) {
        this.dismissLoading();
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successAddJob(BaseBean baseBean) {
        this.dismissLoading();
        AddJobBean jobBean = (AddJobBean) baseBean;
        if (jobBean.getCode() == 0) {
            this.finish();
            ToastUtil.showShortToast(this, "提交成功");
        } else {
            ToastUtil.showShortToast(this, "提交失败:" + jobBean.getMsg());
        }
    }

    /**
     * 年月日选择
     */
    private void showYearMonthPicker() {
        new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = formatter.format(date);
                tvPublishDate.setText(dateString);
            }
        }).setSubmitColor(0xFF007C70).setCancelColor(0xFF999999).build().show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (jobPresenter != null) {
            jobPresenter.onDestroy();
            jobPresenter = null;
        }
    }
}